package com.baosight.risk.service.jlta.impl;

import com.baosight.risk.api.jlta.JltaTeamService;
import com.baosight.risk.common.bean.BeanUtils;
import com.baosight.risk.common.utils.ShiroUtils;
import com.baosight.risk.mapper.jlta.JltaTeamMapper;
import com.baosight.risk.mapper.jlta.JltaTeamUserMapper;
import com.baosight.risk.mapper.xtdc.XtdcDirectoryMapper;
import com.baosight.risk.service.jlta.entity.JltaTeam;
import com.baosight.risk.service.jlta.entity.JltaTeamUser;
import com.baosight.risk.service.xtdc.entity.XtdcDirectory;
import com.baosight.risk.service.xtzy.entity.XtzyUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

/**
 * 作者： lishaoqing
 * 时间： 2019年07月03日
 * 描述： JltaTeamServiceImpl
 * 版本: v1.0
 */
@Service
public class JltaTeamServiceImpl implements JltaTeamService {

    @Autowired
    private JltaTeamMapper jltaTeamMapper;
    @Autowired
    private XtdcDirectoryMapper directoryMapper;
    @Autowired
    private JltaTeamUserMapper jltaTeamUserMapper;

    @Override
    public List<JltaTeam> selectOwnerTeam(String owner) {
        return jltaTeamMapper.selectOwnerTeam(owner);
    }

    @Override
    public List<JltaTeam> selectJoinTeam(JltaTeam jltaTeam, String userCode) {
        return jltaTeamMapper.selectJoinTeam(jltaTeam, userCode);
    }

    @Override
    public Integer addTeam(JltaTeam jltaTeam) {
        String rootDocId = UUID.randomUUID().toString();
        jltaTeam.setRootDocId(rootDocId);
        jltaTeamMapper.insert(jltaTeam);
        XtzyUser user = ShiroUtils.getUser();
        //生成根目录Id
        JltaTeamUser jltaTeamUser = new JltaTeamUser();
        jltaTeamUser.setId(UUID.randomUUID().toString());
        jltaTeamUser.setTeamId(jltaTeam.getId());
        jltaTeamUser.setTeamName(jltaTeam.getTeamName());
        jltaTeamUser.setUserCode(user.getUserCode());
        jltaTeamUser.setUserName(user.getUserDesc());
        jltaTeamUser.setCompCode(user.getExt1());
        jltaTeamUser.setCompName(user.getExt2());
        jltaTeamUser.setRole("2");
        jltaTeamUser.setStatus("0");
        jltaTeamUser.setRoleName("团队创建人");
        jltaTeamUserMapper.insert(jltaTeamUser);
        //产生根目录
        XtdcDirectory directory = new XtdcDirectory();
        directory = (XtdcDirectory) BeanUtils.initBean(directory);
        directory.setId(rootDocId);
        directory.setDirName(jltaTeam.getTeamName());
        directory.setParentGuid("0");
        directoryMapper.insert(directory);
        return 1;
    }

    @Override
    public Integer editTeam(JltaTeam jltaTeam) {
        return jltaTeamMapper.editTeam(jltaTeam);
    }

    @Override
    public Integer deleteTeam(String id) {
        jltaTeamMapper.deleteTeam(id);
        jltaTeamUserMapper.deleteByTeamId(id);
        return 1;
    }

    @Override
    public List<JltaTeamUser> selectUserList(String teamId) {
        return jltaTeamUserMapper.selectUserList(teamId);
    }

    @Override
    public Integer addTeamUser(JltaTeamUser jltaTeamUser) {
        return jltaTeamUserMapper.insert(jltaTeamUser);
    }

    @Override
    public Integer deleteTeamUser(JltaTeamUser jltaTeamUser) {
        return jltaTeamUserMapper.deleteTeamUser(jltaTeamUser);
    }

    @Override
    public List<JltaTeam> searchTeam(String teamName) {
        return jltaTeamMapper.searchTeam(teamName);
    }

    @Override
    public List<JltaTeamUser> selectTeamUserList(JltaTeamUser jltaTeamUser) {
        return jltaTeamUserMapper.selectTeamUserList(jltaTeamUser);
    }

    @Override
    public Integer teamAdmin(String userId) {
        return jltaTeamUserMapper.teamAdmin(userId);
    }

    @Override
    public JltaTeamUser selectOne(JltaTeamUser jltaTeamUser) {
        return jltaTeamUserMapper.selectOne(jltaTeamUser);
    }

    @Override
    public Integer cancelTeamAdmin(String userId) {
        return jltaTeamUserMapper.cancelTeamAdmin(userId);
    }

}
